package com.neusoft.SPNursingHome.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neusoft.SPNursingHome.dao.ClientDao;
import com.neusoft.SPNursingHome.dao.MealSetDao;
import com.neusoft.SPNursingHome.po.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.List;

@Service
public class MealSetServiceImpl implements MealSetService {

    @Autowired
    private MealSetDao mealSetdao;

    @Autowired
    private ClientDao clientDao;

    @Override
    public ResponseBean<List<MealSet>> listAll() {
        List<MealSet> list = mealSetdao.selectList(new QueryWrapper<>());
        return new ResponseBean<>(list);
    }

    @Override
    public ResponseBean<MealSet> getById(Integer id) {
        MealSet ms = mealSetdao.selectById(id);
        if (ms == null) {
            return new ResponseBean<>(404, "未找到对应 MealSet 记录");
        }
        return new ResponseBean<>(ms);
    }

    @Override
    public ResponseBean<IPage<MealSetWithClient>> listAllWithClientName(
            String name, Integer adId, String role, long page, long size) {

        Page<MealSetWithClient> pager = new Page<>(page, size);

        if ("健康管家".equals(role) && adId != null) {
            if (name == null || name.isBlank()) {
                IPage<MealSetWithClient> list = mealSetdao.selectWithClientName1(pager,adId);
                return new ResponseBean<>(list);
            } else {
                IPage<MealSetWithClient> list = mealSetdao.selectWithClientNameByName1(
                        pager, "%" + name.trim() + "%",adId);
                return new ResponseBean<>(list);
            }
        }else{
            if (name == null || name.isBlank()) {
                IPage<MealSetWithClient> list = mealSetdao.selectWithClientName(pager);
                return new ResponseBean<>(list);
            } else {
                IPage<MealSetWithClient> list = mealSetdao.selectWithClientNameByName(
                        pager, "%" + name.trim() + "%");
                return new ResponseBean<>(list);
            }
        }
    }

    @Override
    public ResponseBean<List<Client>> listClientsWithoutMealSet(String name) {
        if (name == null || name.isBlank()) {
            // 不带条件，走原来的全量联表查询
            List<Client> list = mealSetdao.selectClientsWithoutMealSet();
            return new ResponseBean<>(list);
        } else {
            // 带姓名模糊条件的联表查询
            List<Client> list = mealSetdao.listClientsWithoutMealSet("%" + name.trim() + "%");
            return new ResponseBean<>(list);
        }
    }

    @Override
    public ResponseBean<Void> create(MealSet set) {
        System.out.println(">>> MealSetService.create 入参: " + set);

        int rows = mealSetdao.insert(set);

        // 打印 insert 返回值
        System.out.println(">>> MealSetService.create insert 返回 rows = " + rows);

        // 如果是自增主键，打印插入后回填的 setId
        System.out.println(">>> MealSetService.create insert 后 set.getSetId() = " + set.getSetId());

        if (rows != 1) {
            System.out.println(">>> MealSetService.create 错误：新增失败，入参 = " + set);
            return new ResponseBean<>(500, "MealSet 新增失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> update(MealSet set) {
        int rows = mealSetdao.updateById(set);
        if (rows != 1) {
            return new ResponseBean<>(500, "MealSet 更新失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<Void> deleteById(Integer id) {
        int rows = mealSetdao.deleteById(id);
        if (rows != 1) {
            return new ResponseBean<>(500, "MealSet 删除失败");
        }
        return new ResponseBean<>();
    }

    @Override
    public ResponseBean<List<MealSet>> searchByClientName(String name) {
        // 先拿到所有匹配 name 的 client_id 列表
        List<Object> rawIds = clientDao.selectObjs(
                new QueryWrapper<Client>()
                        .select("client_id")
                        .like("client_name", name)
        );
        if (rawIds.isEmpty()) {
            return new ResponseBean<>(Collections.emptyList());
        }
        // 把 Object 强转成 Integer
        List<Integer> clientIds = rawIds.stream()
                .map(id -> (Integer) id)
                .toList();

        // 2) 用 client_id IN (...) 查 MealSet
        List<MealSet> sets = mealSetdao.selectList(
                new QueryWrapper<MealSet>()
                        .in("client_id", clientIds)
        );
        return new ResponseBean<>(sets);
    }
}
